package com.hns.can.common.service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hns.can.common.constants.GlobalConst;

/**
 * 缓存管理器
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Service
public class CacheTaskManager {

	private static final Logger logger = LoggerFactory.getLogger(CacheTaskManager.class);
	
	private static Map<String, Task> taskMap = new ConcurrentHashMap<String, Task>();
	
	@Autowired
	private SysCacheService sysCacheService;
	
	public void addTask(String key, Task task){
		taskMap.put(key, task);
	}
	
	/**
	 * 加载缓存
	 * @param key
	 * @return
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public Object loadCache(String key, String corpId){
		Object result = null;
		if(taskMap.containsKey(key)){
			result = taskMap.get(key).run(corpId);
		}
		return result;
	}
	
	/**
	 * 清除缓存
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	public void cleanCache(String type, String corpId){
		if(GlobalConst.CACHE_KEY_BASE_ORGAN.equals(type)){
			sysCacheService.cleanBaseOrganCache(corpId);
		}else if(GlobalConst.CACHE_KEY_BASE_LINE_INFO.equals(type)){
			sysCacheService.cleanBaseLineInfoCache(corpId);
		}else if(GlobalConst.CACHE_KEY_BASE_CAR_INFO.equals(type)){
			sysCacheService.cleanBaseCarInfoCache(corpId);
		}else if(GlobalConst.CACHE_KEY_BASE_DRV_INFO.equals(type)){
			sysCacheService.cleanBaseDrvInfoCache(corpId);
		}else if(GlobalConst.CACHE_KEY_BASE_EQMT_INFO.equals(type)){
			sysCacheService.cleanBaseEqmtInfoCache(corpId);
		}
		logger.error("已清除缓存"+type);
	}
}
